package com.szd.qiongyou20.home.search.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.szd.qiongyou20.R;
import com.szd.qiongyou20.home.search.bean.AskBean;
import com.szd.qiongyou20.utils.BitmapCircleTransformation;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by SZD on 2016/12/26.
 */

public class Ask_ListView_Adapter extends BaseAdapter {

    private Context mContext;
    private List<AskBean.DataBean> mList;

    public Ask_ListView_Adapter(Context context, List<AskBean.DataBean> list) {
        mContext = context;
        mList = list;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_search_wd, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        // 获取数据
        String avatar = mList.get(position).getAvatar();
        String author = mList.get(position).getAuthor();
        String title = mList.get(position).getTitle();
        String content = mList.get(position).getContent();
        String add_time = mList.get(position).getAdd_time();
        int answer_num = mList.get(position).getAnswer_num();

        // 将数据添加到View中

        Glide.with(mContext).load(avatar)
                .transform(new BitmapCircleTransformation(mContext))
                .into(holder.mImageViewSearchWdHead);
        holder.mTextViewSearchWdAuthor.setText(author);
        holder.mTextViewSearchWdQuestion.setText(title);
        holder.mTextViewSearchWdContent.setText(content);
        holder.mTextViewSearchWdTime.setText(add_time);
        holder.mTextViewSearchWdOverCount.setText(" " + answer_num);

        return convertView;
    }


    static class ViewHolder {
        @Bind(R.id.imageView_search_wd_head)
        ImageView mImageViewSearchWdHead;
        @Bind(R.id.textView_search_wd_author)
        TextView mTextViewSearchWdAuthor;
        @Bind(R.id.textView_search_wd_question)
        TextView mTextViewSearchWdQuestion;
        @Bind(R.id.textView_search_wd_content)
        TextView mTextViewSearchWdContent;
        @Bind(R.id.textView_search_wd_time)
        TextView mTextViewSearchWdTime;
        @Bind(R.id.textView_search_wd_over_count)
        TextView mTextViewSearchWdOverCount;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
